package cn.pconline.autoclub.web.admin;

import cn.pconline.autoclub.entity.ClubLevel;
import cn.pconline.autoclub.entity.ClubUpgradePrize;
import cn.pconline.autoclub.service.ClubLevelService;
import cn.pconline.autoclub.util.Pager;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.gelivable.log.GeliLogFacade;
import org.gelivable.web.Env;
import org.gelivable.web.EnvUtils;
import org.gelivable.webmvc.JSONBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author 作者 guoqiang
 * @version 2014-4-18
 */
@Controller
@RequestMapping("/admin")
public class LevelController {

    @Autowired
    private ClubLevelService clubLevelService;

    /**
     * 车友会等级列表页--等级与奖品设置
     *
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "/level/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp) {
        Env env = EnvUtils.getEnv();
        int pageNo = env.paramInt("pageNum", 1);
        int pageSize = env.paramInt("pageSize", 20);
        String name = env.param("name", "");

        Pager<ClubLevel> pager = clubLevelService.pagerBy(name, pageNo, pageSize);
        req.setAttribute("pager", pager);
        req.setAttribute("name", name);
        return "admin/system/level";
    }

    /**
     * 车友会等级修改页--修改等级信息
     *
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "/level/detail.do")
    public String detail(HttpServletRequest req, HttpServletResponse resp) {
        Env env = EnvUtils.getEnv();
        int levelId = env.paramInt("levelId");
        ClubLevel clubLevel = clubLevelService.getById(levelId);
        List<ClubUpgradePrize> listPrize = clubLevelService.listPrizeByLevelId(levelId);
        req.setAttribute("clubLevel", clubLevel);
        req.setAttribute("listPrize", listPrize);
        return "admin/system/levelDetail";
    }

    /**
     * 保存等级奖品信息--新增奖品
     *
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "/level/updateSave.do")
    public String updateSave(HttpServletRequest req, HttpServletResponse resp) {
        Env env = EnvUtils.getEnv();
        GeliLogFacade.log();
        int statusCode = 200;
        String message = "保存成功";
        long i = clubLevelService.updateSaveLevel(req, env);
        if (i < 1) {
            message = "保存失败";
        }
        return this.callback(resp, statusCode, message, "level-detail", null);
    }

    /**
     * 删除等级奖品信息--删除奖品
     *
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "/level/delete.do")
    public String delele(HttpServletRequest req, HttpServletResponse resp) {
        Env env = EnvUtils.getEnv();
        long id = env.paramLong("id");
        clubLevelService.removePrizeById(id);
        return this.callback(resp, 200, "删除成功", "level-detail", null);
    }

    /**
     * 返回语句块(适用于Geli2.0)
     *
     * @param resp
     * @param statusCode - 返回状态代码，200=成功，300=失败
     * @param message - 操作后的提示内容
     * @param navTabId - 返回的navTabId
     * @param callbackType - 回调方式，例如closeCurrent
     * @return
     */
    public String callback(HttpServletResponse resp, int statusCode, String message, String navTabId, String callbackType) {
        JSONBuilder json = new JSONBuilder();
        resp.setCharacterEncoding("UTF-8");
        if (statusCode > 0) {
            json.put("statusCode", statusCode);
        }
        if (StringUtils.isNotBlank(message)) {
            json.put("message", message);
        }
        if (StringUtils.isNotBlank(navTabId)) {
            json.put("navTabId", navTabId);
        }
        if (StringUtils.isNotBlank(callbackType)) {
            json.put("callbackType", callbackType);
        }
        try {
            resp.getWriter().println(json.toString());
        } catch (IOException ex) {
            //system error.
        }
        return null;
    }
}
